const { getConnection } = require('../config/db');

class FeedbackModel {
  // 添加反馈
  static async addFeedback(openid, content, contact = '') {
    const connection = await getConnection();
    try {
      const sql = 'INSERT INTO feedbacks (openid, content, contact) VALUES (?, ?, ?)';
      const [result] = await connection.execute(sql, [openid, content, contact]);
      
      return {
        id: result.insertId,
        openid,
        content,
        contact,
        status: '待处理',
        created_at: new Date()
      };
    } finally {
      connection.release();
    }
  }

  // 获取反馈列表（管理端）
  static async getFeedbackList(page = 1, pageSize = 20, status = '') {
    const connection = await getConnection();
    try {
      const pageNum = Number.isFinite(Number(page)) && Number(page) > 0 ? Number(page) : 1;
      const pageSizeNum = Number.isFinite(Number(pageSize)) && Number(pageSize) > 0 ? Number(pageSize) : 20;
      const offset = (pageNum - 1) * pageSizeNum;
      
      let sql = `
        SELECT * FROM feedbacks
      `;
      const params = [];
      
      if (status) {
        sql += ' WHERE status = ?';
        params.push(status);
      }
      
      sql += ` ORDER BY created_at DESC LIMIT ${pageSizeNum} OFFSET ${offset}`;
      
      const [rows] = await connection.query(sql, params);
      
      const countSql = status 
        ? 'SELECT COUNT(*) as total FROM feedbacks WHERE status = ?'
        : 'SELECT COUNT(*) as total FROM feedbacks';
      const countParams = status ? [status] : [];
      const [countResult] = await connection.execute(countSql, countParams);
      
      return {
        list: rows,
        total: countResult[0].total,
        page: pageNum,
        pageSize: pageSizeNum
      };
    } finally {
      connection.release();
    }
  }

  // 更新反馈状态（管理端）
  static async updateFeedbackStatus(id, status, reply = '') {
    const connection = await getConnection();
    try {
      const sql = 'UPDATE feedbacks SET status = ?, reply = ?, updated_at = NOW() WHERE id = ?';
      const [result] = await connection.execute(sql, [status, reply, id]);
      return result;
    } finally {
      connection.release();
    }
  }

  // 删除反馈（管理端）
  static async deleteFeedback(id) {
    const connection = await getConnection();
    try {
      const sql = 'DELETE FROM feedbacks WHERE id = ?';
      const [result] = await connection.execute(sql, [id]);
      return result;
    } finally {
      connection.release();
    }
  }

  // 获取用户的反馈列表
  static async getUserFeedbacks(openid, page = 1, pageSize = 20) {
    const connection = await getConnection();
    try {
      const pageNum = Number.isFinite(Number(page)) && Number(page) > 0 ? Number(page) : 1;
      const pageSizeNum = Number.isFinite(Number(pageSize)) && Number(pageSize) > 0 ? Number(pageSize) : 20;
      const offset = (pageNum - 1) * pageSizeNum;
      
      const sql = `
        SELECT * FROM feedbacks
        WHERE openid = ?
        ORDER BY created_at DESC
        LIMIT ${pageSizeNum} OFFSET ${offset}
      `;
      
      const [rows] = await connection.execute(sql, [openid]);
      
      const countSql = 'SELECT COUNT(*) as total FROM feedbacks WHERE openid = ?';
      const [countResult] = await connection.execute(countSql, [openid]);
      
      return {
        list: rows,
        total: countResult[0].total,
        page: pageNum,
        pageSize: pageSizeNum
      };
    } finally {
      connection.release();
    }
  }
}

module.exports = FeedbackModel;

